﻿function createXMLHttpRequest(){
	var xmlhttp;
	if(window.XMLHttpRequest){
		xmlhttp = new XMLHttpRequest();
	}else if(window.ActiveXObject){
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}else{
		return false;
	}
	return xmlhttp;
}
 
function timlop(malop,ID){
	var xmlhttp;
	var e= document.getElementById(ID);
	e.innerHTML="<img src='images/ajax_loading.gif' />";
	xmlhttp=new createXMLHttpRequest();
	if(xmlhttp){
		var url="timlop.php?malop="+ malop;
		xmlhttp.open('GET',url,true);
		xmlhttp.send(null);
		xmlhttp.onreadystatechange=function(){
			if(xmlhttp.readyState==4 && xmlhttp.status==200){
				var e= document.getElementById(ID);
				e.innerHTML=xmlhttp.responseText;
			}
		}
	}else{
		alert("Trinh duyet khong ho tro Ajax");
	}
}

function Phantrang(trang,ID,malop,type){
	var xmlhttp;
	var e= document.getElementById(ID);
	e.innerHTML="<img src='images/ajax_loading.gif' />"; 
	xmlhttp=new createXMLHttpRequest();
	if(xmlhttp){
		var url="timlop.php";
		xmlhttp.open("POST",url,true);
		xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");	
	if(type=1)	xmlhttp.send("first="+trang+" &malop="+malop);
	if(type=2)	xmlhttp.send("page="+trang+" &malop="+malop);
	if(type=3)	xmlhttp.send("page="+trang+" &malop="+malop);
	if(type=4)	xmlhttp.send("page="+trang+" &malop="+malop);
	if(type=5)	xmlhttp.send("page="+trang+" &malop="+malop);
		xmlhttp.onreadystatechange=function(){
			if(xmlhttp.readyState==4 && xmlhttp.status==200){
				var e= document.getElementById(ID);
				e.innerHTML=xmlhttp.responseText;
			}
		}
	}else{
		alert("Trinh duyet khong ho tro Ajax");
	}
}
$(document).ready(function(){
	$('pageSelect').change(function(){
		$(this).closest('form').submit();
	});
	$(document).on('click','.page_number',function(e){
		e.preventDefault();
		var segment =$(this).data('segment');
		var page = $(this).data('page');
		var malop =$('#MaLop').val();
		if(!$(this).parent().hasClass('disabled')){
			loadpage(page,malop,segment,'','');
		}
	});
	$(document).on('click','#previous',function(e){
		e.preventDefault();
		var segment = $(this).data('segment');
		var page = $(this).data('page');
		var malop = $('#MaLop').val();
		if(!$(this).parent().hasClass('disabled')){
			loadpage(page,malop,segment,true,'');
		}
	});
	$(document).on('click','#next',function(e){
		e.preventDefault();
		var segment = $(this).data('segment');
		var page = $(this).data('page');
		var malop = $('#MaLop').val();
		if(!$(this).parent().hasClass('disabled')){
			loadpage(page,malop,segment,'',true);
		}
	});
	function loadpage(page,malop,segment,previous,next){
		var pagam ={'malop':malop,'page':page,'segment':segment,'previous':previous,'next':next};
		var url="timlop.php";
		$.ajax({
			url:url,
			data: pagam,
			type: "POST",
			dataType: "HTML",
			error: function(){},
			beforeSend: function(){			 
				
			},
			complete: function(){
				 
			},
			success: function(data){
				if (data){
					$('#danhsach').html(data);
				}else{
					$('#danhsach').html('');
					var info = "<div class='alert alert-warning'>KhĂ´ng tĂ¬m tháº¥y sinh viĂªn cá»§a lá»›p nĂ y.</div>";
					$('#danhsach').html(info);
				}
				
			}
			
		});
	}
});